﻿Type.registerNamespace('MS.InformationManagement.Ribbon');

MS.InformationManagement.Ribbon.GetItemsIDQueryString = function () {
    var selectedItems = SP.ListOperation.Selection.getSelectedItems();
    var selectedItemsQueryString = '';
    if (selectedItems.length > 0) {
        selectedItemsQueryString = '&itemsID='
        for (var i in selectedItems) {
            var currenItem = selectedItems[i];
            selectedItemsQueryString = selectedItemsQueryString + currenItem.id + '|';
        }
    }
    return selectedItemsQueryString;
}

MS.InformationManagement.Ribbon.EnableButton = function () {

    var items = SP.ListOperation.Selection.getSelectedItems();
    return (items.length > 0);
}

MS.InformationManagement.Ribbon.EnableButtonOneItem = function () {

    var items = SP.ListOperation.Selection.getSelectedItems();
    return (items.length <= 1);
}

MS.InformationManagement.Ribbon.GlobalEnableButton = function () {

    var items = SP.ListOperation.Selection.getSelectedItems();
    return (items.length == 0);
}

MS.InformationManagement.Ribbon.EnableButtonContent = function (contentTypeNames) {

    var items = SP.ListOperation.Selection.getSelectedItems();
    if (items.length == 0)
        return false;
    var result = true;
    items = getSelectedIds();
    var contentTypes = contentTypeNames.split(',')
    var listId = SP.ListOperation.Selection.getSelectedList();
    var data = { selectedIds: items, listIds: listId };
    jQuery.ajax({
        url: "/sites/docs/_vti_bin/MS.InformationManagement.Services/RibbonMethods.asmx/GetItemsContentType",
        type: "POST",
        dataType: "json",
        data: JSON.stringify(data), async: false,
        success: function (response, status) {
            if (response.d.length == 0) {
                result = false;
            }
            else {
                for (var i in response.d) {
                    result = result && $.inArray(response.d[i], contentTypes) != -1;
                }
            }
        },
        error: function (jqXHR, textStatus, errorThrown) {
            var error = JSON.parse(jqXHR.response);
            MS.InformationManagement.Common.ShowStatus(error.Message, 'red', 6000);
            result = false;
        },
        contentType: "application/json; charset=utf-8"
    });

    return result;
}

MS.InformationManagement.Ribbon.EnableSchedulerButtonContent = function () {
    {
        var enable = true;
        var listId = SP.ListOperation.Selection.getSelectedList();
        var items = getSelectedIds();
        if (items.length == 0)
            return false;
        var data = { listId: listId, selectedIds: items }
        jQuery.ajax({
            url: "/sites/docs/_vti_bin/MS.InformationManagement.Services/RibbonMethods.asmx/IsAnyDocumentScheduled",
            type: "POST",
            dataType: "json",
            data: JSON.stringify(data), async: false,
            success: function (result, status) {
                enable = !result.d;
            },
            error: function (jqXHR, textStatus, errorThrown) {
                var error = JSON.parse(jqXHR.response);
                MS.InformationManagement.Common.ShowStatus(error.Message, 'red', 6000);
                enable = false;
            },
            contentType: "application/json; charset=utf-8"
        });
        return enable;
    }
}

function processResult(xData, status) {
    alert(xData.responseText);
}

function getSelectedIds() {
    var listId = SP.ListOperation.Selection.getSelectedItems();
    var selectedIds = new Array();
    for (var i in listId) {
        selectedIds.push(parseInt(listId[i].id));
    }
    return selectedIds;
}


MS.InformationManagement.Ribbon.EnableButtonAnular = function () {
    {
        var enable = true;
        var listId = SP.ListOperation.Selection.getSelectedList();
        var items = getSelectedIds();
        var data = { selectedIds: items, listId: listId };

        if (items.length == 0)
            return true;

        if (items.length > 1)
            return false;

        jQuery.ajax({
            url: "/sites/docs/_vti_bin/MS.InformationManagement.Services/RibbonMethods.asmx/PuedoAnular",
            type: "POST",
            dataType: "json",
            data: JSON.stringify(data), async: false,
            success: function (response, status) {
                enable = response.d;
            },
            error: function (jqXHR, textStatus, errorThrown) {
                var error = JSON.parse(jqXHR.response);
                MS.InformationManagement.Common.ShowStatus(error.Message, 'red', 6000);
                enable = false;
            },
            contentType: "application/json; charset=utf-8"
        });
        return enable;
    }
}